/*
@最初版本开发日期: 2009年07月08日
@著作权所有: Lou Barnes ( http://www.loubarnes.com )

报告漏洞，意见或建议, 请联系 Lou Barnes(iua1024@gmail.com) 或登陆 http://www.loubarnes.com
*/


var win = {
	
	m_sType : "dialog",	// 可取值: dialog 对话框 / warning 警告 / error 错误
	m_iWidth : 400,
	m_iHeight : 120,
	m_iTop : 0,
	m_iLeft : 0,
	
	m_oData : null,
	
	loadURL : function(sTitle, sURL, oOption)
	{
		this.show(sTitle, '', oOption);
				
		$('#win-content').addClass('winloading');
		$('#win-content').html('<div class="ajax-wait">&nbsp;</div>');
		
		$('#win-wait').show();
		$('#win-content input').attr('disabled', true);
		$('#win-content textarea').attr('disabled', true);
		$('#win-xontent button').attr('disabled', true);
		
		
		var _self = this;
		$.ajax({
			type: "GET",
			url: sURL,
			success: function(msg){
				$('#win-content').removeClass('winloading');
				$('#win-wait').hide();
				$('#win-content input').attr('disabled', false);
				$('#win-content textarea').attr('disabled', false);
				$('#win-content button').attr('disabled', false);
				
				$('#win-content').html(msg);
			}
		});
	},

		
	openURL : function(sTitle, sURL, oOption)
	{
		this.setOpt(oOption);
		
		var iBrowserWidth = 0;
		var iBrowserHeight = 0;
		if ($.browser.msie)
		{  
			iBrowserWidth = document.compatMode == "CSS1Compat"?document.documentElement.clientWidth:document.body.clientWidth;
			iBrowserHeight = document.compatMode == "CSS1Compat"?document.documentElement.clientHeight:document.body.clientHeight;  
		} else {  
			iBrowserWidth = document.documentElement.clientWidth;
			iBrowserHeight = document.documentElement.clientHeight;
		}

		var iLeft = (iBrowserWidth-this.m_iWidth)/2;
		var iTop = (iBrowserHeight-this.m_iHeight)/2;

		window.open(url, "eventDialog", "width="+m_iWidt+", left="+iLeft+", top="+iTop+", toolbar=no, menubar=no, location=no, status=no, scrollbars=yes");
	},
	
	show : function(sTitle, sContent, oOption)
	{
		this.setOpt(oOption);
	
		var e = document.getElementById('win');
		if(!e){
			e = document.createElement('div');

			var sHtml  = '';
			sHtml += '<div id="win" class="' + this.m_sType + '" style="top: 0px; display: none;">';
			sHtml += '	<div id="win-tl"></div>';
			sHtml += '	<div id="win-tm"></div>';
			sHtml += '	<div id="win-tr"></div>';
			sHtml += '	<div style="clear: both;"></div>';

			sHtml += '	<div id="win-ml"></div>';
			sHtml += '	<div id="win-content-outer">';
			sHtml += '		<div id="win-content-top">';
			sHtml += '			<a href="javascript:void(0);" onclick="win.hide();" id="win-close-btn">Close</a>';
			sHtml += '			<div id="win-logo">'+sTitle+'</div>';
			sHtml += '		</div>';
			sHtml += '		<div id="win-content">'+sContent+'</div>';
			sHtml += '	</div>';
			sHtml += '	<div id="win-mr"></div>';
			sHtml += '	<div style="clear: both;"></div>';

			sHtml += '	<div id="win-bl"></div>';
			sHtml += '	<div id="win-bm"></div>';
			sHtml += '	<div id="win-br"></div>';
			sHtml += '	<div style="clear: both;"></div>';
			sHtml += '</div>';
			
			e.innerHTML = sHtml;
			document.body.appendChild(e);
		} else {
			$('#win-content').removeClass('winloading');
			
			$('#win-logo').html(sTitle);
			$('#win-content').html(sContent);
		}

		$('#win').width(this.m_iWidth + 40);
		$('#win-content-outer, #win-content-top, #win-content').width(this.m_iWidth);
		$('#win-bm, #win-tm').width(this.m_iWidth);
		
		
		var myWidth = 0, myHeight = 0;
		myWidth  = $(window).width();
		myHeight = $(window).height();
		


		var yPos;
		if(this.m_iTop>0)
		{
			yPos = this.m_iTop;
		}
		else
		{
			yPos = $(window).height() - 30;
		}
		
		var leftPos;
		if(this.m_iLeft>0)
		{
			leftPos = this.m_iLeft;
		}
		else
		{
			leftPos = (myWidth - this.m_iWidth - 40)/2;
		}
		
		var allElems = document.getElementsByTagName?document.getElementsByTagName("*"):document.all; // or test for that too
		var maxZIndex = 0;
	
		for(var i=0;i<allElems.length;i++) {
			var elem = allElems[i];
			var style = null;
			if (elem.currentStyle) 
				style = elem.currentStyle;
			else if (document.defaultView && document.defaultView.getComputedStyle) {
				style = document.defaultView.getComputedStyle(elem,"");
			}
	
			var sNum;
			if (style) {
				sNum = Number(style.zIndex);
			} else {
				sNum = Number(elem.style.zIndex);
			}
			if (!isNaN(sNum)) {
				maxZIndex = Math.max(maxZIndex,sNum);
			}
		}
		
		$('#win').css('zindex', maxZIndex + 1);

		$("#win").css('marginTop', ($(document).scrollTop() + 10 +(yPos - this.m_iHeight)/2) + (20)+'px');
		
		$('#win').css('height', this.m_iHeight).css('left', leftPos);
		$('#win-content').css('height', (this.m_iHeight - 50)) // - 30px 标题和 20px 边框
								 .css('width', (this.m_iWidth - 20));  // -20px 边框
		$('#win-content-outer').css('height', this.m_iHeight);
		$('#win-tm, #win-bm').css('width', this.m_iWidth);
		$('#win-ml, #win-mr').css('height', this.m_iHeight);
		
		$('#win').show();
	},


	
	hide : function()
	{
		if($('#win-action').get().length > 0)
			$('#win-action').animate({ bottom: "-40px"}, 200 , '', function(){
				$('#win').fadeOut('fast', function(){$(this).parent().remove();});
			});
		else
			$('#win').fadeOut('fast', function(){$(this).parent().remove();});
	},
	
	loading: function(sTitle, oOption)
	{
		this.show(sTitle, '', oOption);
		$('#win-content').addClass('winloading');
		$('#win-content').html('<div class="ajax-wait">&nbsp;</div>');
		$('#win-action').remove();
	},
	
	actions : function(action)
	{
		var html = '<div id="win-action">'; 
		html += '<table><tr>';
		html += '<td align="right" valign="middle">';
		html += action;
		html += '</td></tr></table></div>';
		
		$('#win-action').remove();
		$('#win-content-outer').append(html);
		_height = $('#win-content').height();
		_height = _height - 34;
		$('#win-content').height( _height );
		
		$('#win-action').animate({bottom: "0px"}, 200 );
	},
	
	resize : function()
	{
		var iCurrentHeight = $('#win-mr').height();
		var iReduceHeight = (iCurrentHeight - this.m_iHeight) /2; 
		

		$('#win').animate({marginTop: "+="+ iReduceHeight +"px"}, 200 );
		//$('#win-tl,#win-tm,#win-tr').animate({marginTop: "+="+ reduceHeight +"px"}, 200 );
		
		$('#win-mr,#win-ml,#win-content-outer').animate({height: this.m_iHeight +"px"}, 200 );
		$('#win-content').animate({height: this.m_iHeight - 50 +"px"}, 200 );
	},
	
	
	setOpt : function(oOption)
	{
		if(typeof oOption=="object")
		{
			if(oOption.type) this.m_sType = oOption.type;
			if(oOption.width) this.m_iWidth = oOption.width;
			if(oOption.height) this.m_iHeight = oOption.height;
			if(oOption.top) this.m_iTop = oOption.top;
			if(oOption.left) this.m_iLeft = oOption.left;
		}
	}
	
}
